const mysql = require('koa-mysql'); // 引入数据库连接中间件

const pool = mysql.createPool({
  host: '127.0.0.1',
  user: 'root',
  password: 'root',
  database: 'community_uni',
  charset:'utf8mb4'
})

// 封装promise
const query = function (sql, values) {
  return new Promise((resolve, reject) => {
    pool.getConnection(function (err, connection) {
      if (err) {
        reject(err)
      } else {
        connection.query(sql, values, (err, rows) => {
          if (err) {
            reject(err)
          } else {
            let res = JSON.parse(JSON.stringify(rows)) || {}
            resolve(res)
          }
          connection.release()
        })
      }
    })
  })
}
//   const getSchoolMod =  async () => {
//     // 查数据库
//     const res = await query('select * from schoolinfo')
//     console.log(res);
//   }
// getSchoolMod('select * from schoolinfo')
module.exports = query

